package com.ywk.pattern._24_chain;

public abstract class Handler {
    protected Handler successor;

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }

    public abstract void handlerRequest(int request);
}

class ConcreteHandler1 extends Handler {

    @Override
    public void handlerRequest(int request) {
        if (request >= 0 && request <= 10) {
            System.out.println("handler1");

        } else {
            successor.handlerRequest(request);
        }
    }
}

class ConcreteHandler2 extends Handler {

    @Override
    public void handlerRequest(int request) {
        if (request >= 11 && request <= 20) {
            System.out.println("handler2");

        } else {
            successor.handlerRequest(request);
        }
    }
}

class ConcreteHandler3 extends Handler {

    @Override
    public void handlerRequest(int request) {
        if (request >= 21 && request <= 30) {
            System.out.println("handler3");

        } else {
            successor.handlerRequest(request);
        }
    }
}

class Main {
    public static void main(String[] args) {
        ConcreteHandler1 handler1 = new ConcreteHandler1();
        ConcreteHandler2 handler2 = new ConcreteHandler2();
        ConcreteHandler3 handler3 = new ConcreteHandler3();
        handler1.setSuccessor(handler2);
        handler2.setSuccessor(handler3);
        int[] requests = {1, 11, 21, 18, 30};
        for (int request : requests) {
            handler1.handlerRequest(request);
        }
    }
}
